//
//  PublicityViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/6.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "PublicityViewController.h"
#import "PublicityTableViewCell.h"
#import "PublicityDetailViewController.h"
#import "MessageViewController.h"
#import "NetErrorView.h"
@interface PublicityViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSMutableArray * cellData;
@property (nonatomic, strong)UILabel * projectNameLab;
@property (nonatomic, strong)NetErrorView * netErrorView;
@end

@implementation PublicityViewController(networking)
- (void)publicityData{
    NSDictionary * dic = @{
                           
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            
        }else{
//            ShowMessage(responseObject[@"msg"]);
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation PublicityViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.netErrorView = [[NetErrorView alloc] initWithFrame:CGRectMake(0, NAVI_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAVI_HEIGHT - TABBAR_HEIGHT)];
//    [self.view addSubview:self.netErrorView];
    [self.netErrorView showView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    //右侧消息按钮
    UIButton * naviRightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40/WIDTH_6_SCALE, 20/WIDTH_6_SCALE)];
    [naviRightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [naviRightBtn setTitle:@"消息" forState:UIControlStateNormal];
    [naviRightBtn addTarget:self action:@selector(naviRightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    naviRightBtn.titleLabel.font = DEF_FontSize_12;
    UIBarButtonItem * rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviRightBtn];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}
- (void)viewWillDisappear:(BOOL)animated{
    [self.netErrorView dismiss];
}
#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TABBAR_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.headerView;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 101/WIDTH_6_SCALE;
        [_tableView registerClass:[PublicityTableViewCell class] forCellReuseIdentifier:NSStringFromClass([PublicityTableViewCell class])];
    }return _tableView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 33/WIDTH_6_SCALE)];
        _headerView.backgroundColor = getColor(@"f5f5f5");
        UILabel * warningLab = [[UILabel alloc] initWithFrame:CGRectMake(15/WIDTH_6_SCALE, 0, SCREEN_WIDTH - 15/WIDTH_6_SCALE, _headerView.height)];
        warningLab.text = @"为保护受助者个人隐私，公示结束后将隐藏其个人信息";
        warningLab.font = DEF_FontSize_12;
        warningLab.textColor = [UIColor blackColor];
        [_headerView addSubview:warningLab];
    }return _headerView;
}
- (NSMutableArray *)cellData{
    if (!_cellData) {
        _cellData = @[@{@"name":@"王", @"money":@"10000元", @"time":@"2018-01-01"},
                      @{@"name":@"宝", @"money":@"10000元", @"time":@"2018-01-01"},
                      @{@"name":@"强", @"money":@"10000元", @"time":@"2018-01-01"},].mutableCopy;
    }return _cellData;
}
#pragma mark - action
- (void)naviRightBtnClick:(UIButton *)sender{
    MessageViewController * vc = [[MessageViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PublicityTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PublicityTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.cellData[indexPath.section];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.cellData.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * whiteBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 42/WIDTH_6_SCALE)];
    whiteBGView.backgroundColor = [UIColor whiteColor];
    self.projectNameLab = [[UILabel alloc] initWithFrame:CGRectMake(16/WIDTH_6_SCALE, 0, SCREEN_WIDTH - 32/WIDTH_6_SCALE, whiteBGView.height)];
    self.projectNameLab.text = @"中老年抗癌计划";
    self.projectNameLab.textColor = [UIColor blackColor];
    self.projectNameLab.font = DEF_FontSize_14;
    self.projectNameLab.textAlignment = NSTextAlignmentLeft;
    [whiteBGView addSubview:self.projectNameLab];
    return whiteBGView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 42/WIDTH_6_SCALE;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * grayBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 9/WIDTH_6_SCALE)];
    grayBGView.backgroundColor = getColor(@"f5f5f5");
    return grayBGView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 9/WIDTH_6_SCALE;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PublicityDetailViewController * vc = [[PublicityDetailViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
